﻿


namespace IntegrationTests.ConnectivityTests
{
    using System.DirectoryServices.Protocols;
    using  DirectoryServicesProtocolsExtensions.LdapConnection;
    using DirectoryServicesProtocolsExtensions.LdapConnection.Validation;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class ConnectivityTests : DirectoryTest
    {
        [TestInitialize]
        public void TestInitializeActiveDirectoryTests()
        {
            TestInitialize();
        }

        [TestMethod]
        [TestCategory("ActiveDirectory")]
        public void IsHostOrDomainBindableWithValidConnectionParameters()
        {
            var ldapConnectionFactory = new LdapConnectionFactory(LdapConnectionPropertiesContainer);
            var ldapConnection = ldapConnectionFactory.Create();
            ldapConnection.IsHostOrDomainBindable(); // thows exception if fails
        }

        [TestMethod]
        [TestCategory("ActiveDirectory")]
        public void IsHostOrDomainBindableWithBasicAuth()
        {
            LdapConnectionPropertiesContainer.ConnectionAccountName = "cn=administrator,cn=users,dc=fabrikam,dc=com";
            LdapConnectionPropertiesContainer.AuthType = AuthType.Basic;
            
            var ldapConnectionFactory = new LdapConnectionFactory(LdapConnectionPropertiesContainer);
            var ldapConnection = ldapConnectionFactory.Create();
            ldapConnection.IsHostOrDomainBindable(); // thows exception if fails
        }

        /* Todo: domain\netbios name type of account name fails
        [TestMethod]
        public void IsHostOrDomainBindableWithValidNetBiosStyleUserName()
        {
            var ldapConnectionPropertiesContainer = new MockFabrikamActiveDirectoryConnectionPropertiesContainer
            {
                UseProcessSecurityContextFlag = false
            };
            ldapConnectionPropertiesContainer.ConnectionAccountName = "redmond" + "\\" + ldapConnectionPropertiesContainer.ConnectionAccountName;

            var ldapConnectionFactory = new LdapConnectionFactory(ldapConnectionPropertiesContainer);
            var ldapConnection = ldapConnectionFactory.Create();
            ldapConnection.IsHostOrDomainBindable(); // thows exception if fails
        }
        */

        [TestMethod, TestCategory("ActiveDirectory")]
        public void IsHostOrDomainBindableWithValidUPNStyleUserName()
        {
            LdapConnectionPropertiesContainer.UseProcessSecurityContextFlag = false;
            LdapConnectionPropertiesContainer.ConnectionAccountName = 
                LdapConnectionPropertiesContainer.ConnectionAccountName + "@fabrikam.com";

            var ldapConnectionFactory = new LdapConnectionFactory(LdapConnectionPropertiesContainer);
            var ldapConnection = ldapConnectionFactory.Create();
            ldapConnection.IsHostOrDomainBindable(); // thows exception if fails
        }

        [TestMethod]
        [TestCategory("ActiveDirectory")]
        [TestCategory("EDirectory")]
        public void IsSchemaPointerAccessibleAnonymously()
        {
            LdapConnectionPropertiesContainer.ConnectionAccountName = null;
            LdapConnectionPropertiesContainer.ConnectionAccountPassword = null;
        
            var ldapConnectionFactory = new LdapConnectionFactory(LdapConnectionPropertiesContainer);
            var ldapConnection = ldapConnectionFactory.Create();
            Assert.IsNotNull(ldapConnection.GetSubschemaSubentryDN());
        }

        [TestMethod]
        [TestCategory("ActiveDirectory")]
        [TestCategory("EDirectory")]
        public void IsSchemaAccessibleWithValidCredentials()
        {                                          
            var ldapConnectionFactory = new LdapConnectionFactory(LdapConnectionPropertiesContainer);
            var ldapConnection = ldapConnectionFactory.Create();
            var ldapMetaMapFactory = new LdapMetaMapFactory(new LdapMetaMapFactoryInitializationValues());
            Assert.IsNotNull(ldapConnection.GetAttributeMetaData("cn", ldapMetaMapFactory));
        }

        [TestMethod, TestCategory("Slow")]
        [ExpectedException(typeof(LdapException))]
        public void IsSchemaInAccessibleWithInvalidCredentials()
        {
            LdapConnectionPropertiesContainer.ConnectionAccountPassword = "wrongPassword";
            var ldapConnectionFactory = new LdapConnectionFactory(LdapConnectionPropertiesContainer);
            var ldapConnection = ldapConnectionFactory.Create();
            var ldapMetaMapFactory = new LdapMetaMapFactory(new LdapMetaMapFactoryInitializationValues());
            ldapConnection.GetAttributeMetaData("cn", ldapMetaMapFactory);
        }

        [TestMethod, TestCategory("Slow")]
        [ExpectedException(typeof(LdapException))]
        public void IsHostOrDomainNotBindableWithInvalidDomainNameParameter()
        {
            LdapConnectionPropertiesContainer.HostOrDomainName = "idontexist.com";
            var ldapConnectionFactory = new LdapConnectionFactory(LdapConnectionPropertiesContainer);
            var ldapConnection = ldapConnectionFactory.Create();
            ldapConnection.IsHostOrDomainBindable();
        }

        [TestMethod, TestCategory("Slow")]
        [ExpectedException(typeof(LdapException))]
        public void IsHostOrDomainNotBindableWithInvalidPortNameParameter()
        {
            LdapConnectionPropertiesContainer.LdapHostPortNumber = 100;
            var ldapConnectionFactory = new LdapConnectionFactory(LdapConnectionPropertiesContainer);
            var ldapConnection = ldapConnectionFactory.Create();
            ldapConnection.IsHostOrDomainBindable();
        }
    }
}
